import dayjs from 'dayjs'

/**
 * datePicker组件默认选择当前时间
 * @param {Array} jsonRule 规则json
 * @returns 返回新的规则json
 */
export function handleDefaultCurDate(jsonRule) {
  return jsonRule.map((rule) => {
    if (rule.type == 'datePicker') {
      if (rule.props == undefined) {
        rule.props = {}
      }
      if (rule.props.isDefaultCurDate == true) {
        // 打开默认显示当前时间
        const curDate = dayjs(new Date())
        let curDateStr = ''
        if (rule.props.type == 'year') {
          curDateStr = curDate.format('YYYY')
        } else if (rule.props.type == 'month') {
          curDateStr = curDate.format('YYYY-MM')
        } else if (rule.props.type == 'date' || rule.props.type == undefined) {
          curDateStr = curDate.format('YYYY-MM-DD')
        } else if (rule.props.type == 'datetime') {
          curDateStr = curDate.format('YYYY-MM-DD HH:mm:ss')
        } else {
          curDateStr = ''
        }
        rule.value = curDateStr
        rule.props['v-model'] = curDateStr
      }
    }
    return rule
  })
}
